import { RootThunkAction } from '@/types/store'
import { FormData, Token } from '@/types/data'
import request from '@/utils/request'
import { setToken } from '@/utils/token'
type Res = {
  data: Token
  message: string
}
export const loginAction = (formData: FormData): RootThunkAction => {
  return async (dispatch, getState) => {
    /**
     * 1.调用接口获取token
     * 2.存到redux=>再次dispatch=》命令reducer存储token
     */

    const res: Res = await request.post('/authorizations', formData)
    console.log('token', res.data)
    // 这是存储到redux
    dispatch({ type: 'token/login', payload: res.data })
    // 持久化就需要存储到本地
    setToken(res.data)
  }
}
// 发送验证码
export const sendCodeAction = (mobile: string): RootThunkAction => {
  return async (dispatch, getState) => {
    // 验证码不需要存储到redux中，所有只发请求即可
    await request.get(`/sms/codes/${mobile}`)
  }
}
